/**
 * 
 * 网站咨询接口
 * 
 */
var express = require('express');
var router = express.Router();

var sql = require('../db/index');

/**
 *@swagger
 *       /catageory/saveOrUpdate_message:
 *       
 *       post:
 *           description: 咨询分类的新增或编辑接口
 *           tags: [资讯分类]
 *           produces:
 *               - application/x-www-form-urlencoded
 *           parameters:
 *               - name: username
 *                 description: 资讯id
 *                 required: true
 *                 type: number
 *               - name: name,
 *                 description: 资讯名称
 *                 required: true
 *                 type: string
 *               - name: description,
 *                 description: 资讯描述
 *                 required: false
 *                 type: string
 *               - name: no,
 *                 description: 资讯数量
 *                 required: true
 *                 type: number
 *           responses:
 *               200:
 *                   description: 资讯分类新增成功
 *               
 *               500:
 *                   description: 服务器错误
 */

// 资讯分类的新增或编辑接口 req.body
router.post('/catageory/saveOrUpdate_message',async function(req,res) {
    // 获取前端传递的参数
     let {id,name,description,no} = req.body;
     let mysql = id ? 
     `update cms_category set name = '${name}', description = '${description}', no = '${no}' where id=${id}` :
     `insert into cms_category (id, name, description, no) values(null, '${name}', '${description}', '${no}')`;

     sql.query(mysql);

    // 给前端做出响应
    res.send({
        status:200,
        message:"资讯分类新增成功",
        data:null,
        timestamp:new Date().getTime()
    })    
})

//////////////////////////////////////////////////////////////////////////////////////

/**
 * @swagger
 *      /catageory/delete:
 *      post:
 *          description: 删除资讯分类
 *          tags: [资讯分类]
 *          parameters:
 *              - in: query
 *                name: id
 *                description: 资讯id
 *                required: true
 *                schema:
 *                  type: number
 *              - in: query
 *                name: message_status
 *                description: 资讯状态
 *                required: true
 *                schema:
 *                  type: string
 *          responses:
 *             '200':
 *                description: 返回资讯列表
 */

//  删除接口 
router.post('/catageory/delete', async(req,res) => {
    let { id }= req.body;
    sql.query(`delete from cms_category where id = ${id}`);
    res.send({
        status:200,
        message:'删除成功',
        data:null,
        timestamp:new Date().getTime()
    })
})
//////////////////////////////////////////////////////////////////////////////////////





/**
 * @swagger
 *       /catageory/sendOrDelete:
 *       
 *       post:
 *           description: 发布风采或删除接口
 *           tags: [资讯信息]
 *           produces:
 *               - application/x-www-form-urlencoded
 *           parameters:
 *               - name: username
 *                 description: 序号
 *                 required: true
 *                 type: number
 *               - name: message_photo,
 *                 description: 封面
 *                 required: true
 *                 type: string
 *               - name: title,
 *                 description: 风采
 *                 required: true
 *                 type: number
 *               
 *           responses:
 *               200:
 *                   description: 资讯信息发布成功
 *               
 *               500:
 *                   description: 服务器错误
 */

// 资讯信息发布和删除接口 req.body
router.post('/catageory/sendOrDelete',async(req,res)=>{
  // 获取前端传递的参数
  const {id,title} = req.body;
  //如果id存在，修改操作，否则就是新增
  let mysql = '';
  if(id){
    
  }else{
      mysql = `insert into cms_article (id,title) 
      values(null,'${title}')`
  }
  await sql.query(mysql);

  // 给前端做出响应
  res.send({
      status:200,
      message:"风采发布成功成功",
      data:null,
      timestamp:new Date().getTime()
  })    
})
//  删除接口 
router.post('/deleteById_2', async(req,res) => {
    let { id }= req.body;
    await sql.query(`delete from cms_article where id = ${id}`);
    res.send({
        status:200,
        message:'删除成功',
        data:null,
        timestamp:new Date().getTime()
    })
})





/**
 * @swagger
 *      /catageory/review:
 *      post:
 *          description: 推荐或审核资讯信息
 *          tags: [资讯信息]
 *          parameters:
 *              - in: query
 *                name: id
 *                description: 序号
 *                required: true
 *                schema:
 *                  type: number
 *              - in: query
 *                name: message_status
 *                description: 资讯状态
 *                required: true
 *                schema:
 *                  type: string
 *          responses:
 *             '200':
 *                description: 返回资讯信息列表
 */

//  资讯信息推荐和审核接口   需要前端传递参数 id status
//   id是为了知道哪条资讯信息被修改状态  status 知道点击的 是推荐或者审核
router.post('/review',async(req,res)=>{
    //获取前端传递的参数
      let {id,status}=req.body;
      if(status === '审核'){
          if(status === '审核通过'){
               status === '审核通过'
          }else{
             status === '审核不通过'
          }
      }else if(status === '推荐'){
             status = '推荐'
      }else{
             status = '未推荐'
      }
      await sql.query(`update cms_article set status = '${status}' where id = ${id}`);
      //给前端做相应
      res.send({
          status:200,
          message:status=='未审核'?'审核成功':'审核失败',
          data:null,
          timestamp:new Date().getTime()
        })

})

module.exports = router;
